< previous page page_179 next page >

Page 179
result = number1 / number2 [/ number3]

'The following line returns 1.333333
Print 4 / 3
Integer division divides one number into another and then returns only the integer portion of the result. The operator for integer division is the backward slash (\):
result = number1 \ number2 [\ number3]

'The following line returns 1
Print 4 \ 3
Modulus, or remainder, division divides one number into another and returns what is left over after you have obtained the largest integer quotient possible. The modulus operator is the word mod:
result = number1 mod number2 [mod number3]

'The following line returns 2, the remainder when dividing 20 by 3
Print 20 mod 3
As with the case of addition, subtraction, and multiplication, if you divide more than two numbers, each number pair must be separated by a division operator. Also, like the other operations, multiple operators are handled by reading the equation from left to right.
Figure 9.1 shows a simple form that is used to illustrate the differences between the various division operators. The code for the command button of the form is shown as follows:
inpt1 = Text1.Text
inpt2 = Text2.Text
Text3.Text = inpt1 / inpt2
Text4.Text = inpt1 \ inpt2
Text5.Text = inpt1 Mod inpt2
0179-01.jpg
Figure 9.1
This program demonstrates the difference between
Visual Basic's three types of division operators.
After you set up the form, run the program, enter 5 in the first text box and 3 in the second text box, and then click the command button. Notice that different numbers appear in each of the text boxes used to display the results. You can try this example with other number combinations as well.
Using Multiplication and Division in a Program
As a demonstration of how multiplication and division might be used in a program, consider the example of a program to determine the

 
< previous page page_179 next page >